package com.ttg.common.entities;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.ttg.common.enums.SensitivityTypeEnum;
import com.ttg.common.utils.SensitivityUtil;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Objects;

@NoArgsConstructor
@AllArgsConstructor
public class SensitivitySerializer extends JsonSerializer<String> implements ContextualSerializer {
    private SensitivityTypeEnum sensitivityTypeEnum;
    private Integer prefixNoMaskLen;
    private Integer suffixNoMaskLen;
    private String maskStr;
    @Override
    public void serialize(String origin,final JsonGenerator jsonGenerator,final SerializerProvider serializerProvider) throws IOException {
        if (StringUtils.isNotBlank(origin)&&null!=sensitivityTypeEnum){
            switch (sensitivityTypeEnum){
                case NAME:
                    jsonGenerator.writeString(SensitivityUtil.hideChineseName(origin));

                    break;
                case ID_CARD:
                    jsonGenerator.writeString(SensitivityUtil.hideIdCard(origin));
                    break;
                case EMAIL:
                    jsonGenerator.writeString(SensitivityUtil.email(origin));

                    break;
                case PHONE:
                    jsonGenerator.writeString(SensitivityUtil.phone(origin));

                    break;
                case DES_VALUE:
                    jsonGenerator.writeString(SensitivityUtil.desValue(origin,prefixNoMaskLen,suffixNoMaskLen,maskStr));

                    break;
            }
        }else {
            jsonGenerator.writeString("");
        }
    }

    @Override
    public JsonSerializer<?> createContextual(final SerializerProvider serializerProvider,final BeanProperty beanProperty) throws JsonMappingException {
        if (beanProperty!=null){
            if(Objects.equals(beanProperty.getType().getRawClass(),String.class)){
                SensitivityEncrypt sensitivityEncrypt=beanProperty.getAnnotation(SensitivityEncrypt.class);
                if (sensitivityEncrypt==null){
                    sensitivityEncrypt=beanProperty.getContextAnnotation(SensitivityEncrypt.class);
                }
                if(sensitivityEncrypt!=null){
                    return new SensitivitySerializer(sensitivityEncrypt.type(),
                            sensitivityEncrypt.prefixNoMaskLen(),
                            sensitivityEncrypt.suffixNoMaskLen(),
                            sensitivityEncrypt.maskStr());
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType());
        }
        return serializerProvider.findNullValueSerializer(null);

    }
}
